/**
 * @desc forum
 */
jQuery(function ($) {
	
	/* @desc edit */
	
	$('#wrapper #page_forum .topic_list a.comment-edit').bind('click',function(){

		var _id = $(this).data('id');
		var _post = $(this).closest('.topic_list');
		
		var _editform = $('#form-forum_edit');
		
		var _title = _post.find('.post_title');
		var _content = _post.find('.post_content');

		_editform.find('#forum_edit-title').val(_title.text());
		_editform.find('#forum_edit-content').val(_content.text());

		
		$('#dialog-edit').dialog({
            modal: false,
            autoOpen: true,
            height: 500,
            width: 470,
            resizable: false,
            draggable: false,
            buttons: [{
                html: '<span class="m-t-1 sprite-icons sprite-save_on"></span>&nbsp;Save',
            	click: function(){
            		/* TODO: ajax process */
            		var _dialog = $(this);
            		var _data = $('#form-forum_edit').serialize();
            		$.ajax({
                		url: _root + 'ajax/forum/?act=edit'+ _ajaxext,
                		type: 'post',
                		cache: false,
                		dataType : 'json',
                		data: _data + '&id='+_id,
                		success: function(json){
                			if(json.xhr_status == 1) {
                				_title.text(_editform.find('#forum_edit-title').val());
                				_content.text(_editform.find('#forum_edit-content').val());                				
                				_dialog.dialog("close");
                			}
                				
                			if(json.xhr_status == 0) {
                                $("#modal_message").attr('title', json.header).html(json.message);
                				openModal();
                			}
                		}
                	});
                }},
                {
                html:'<span class="m-t-1 sprite-icons sprite-cancel"></span>&nbsp;Cancel',
                click: function(){
                    $(this).dialog("close");
                }
                }]
        });

		return false;
	});
	
	/* delete */
	
	$('#wrapper #page_forum .topic_list a.comment-delete').bind('click',function(){
		
		var _id = $(this).data('id');
		
		$('#dialog-confirm').dialog({
			resizable: false,
			height:200,
			modal: true,
			buttons: {
				"Sil": function() {
					$( this ).dialog( "close" );


		$.ajax({
    		url: _root + 'ajax/forum/?act=delete'+ _ajaxext,
    		type: 'post',
    		cache: false,
    		dataType : 'json',
    		data: 'id='+_id,
    		success: function(json){
    			if(json.xhr_status == 1) {
					$('#post-'+_id).next().remove();
    				$('#post-'+_id).slideFadeToggle(750,'swing');
    			}
    				
    			if(json.xhr_status == 0) {
                    $("#modal_message").attr('title', json.header).html(json.message);
    				openModal();
    			}
    		}
    	});	
				},
				"Vazgeç": function() {
					$( this ).dialog( "close" );
					return false;
				}
			}
		});
		return false;
	});
	
	/* new */
	
	$('#forum_menu a.comment-new').bind('click',function(){

		$('#dialog-new').dialog({
            modal: false,
            autoOpen: true,
            height: 500,
            width: 470,
            resizable: false,
            draggable: false,
            buttons: [{
                html: '<span class="m-t-1 sprite-icons sprite-add"></span>&nbsp;Add New Topic',
            	click: function(){
            		var _dialog = $(this);
            		var _data = $('#form-forum_new').serialize();
            		$.ajax({
                		url: _root + 'ajax/forum/?act=new'+ _ajaxext,
                		type: 'post',
                		cache: false,
                		dataType : 'json',
                		data: _data ,
                		success: function(json){
                			if(json.xhr_status == 1) {
                        		var _json = $('#form-forum_new').serializeObject();

              				$('#page_forum').jqoteapp('#page_forum #tmpl_new_message',_json);
              				$.scrollTo( '#page_forum .st_bottom', 1000 );
                				_dialog.dialog("close");
                			}
                				
                			if(json.xhr_status == 0) {
                                $("#modal_message").attr('title', json.header).html(json.message);
                				openModal();
                			}
                		}
                	});
                }},
                {
                html:'<span class="m-t-1 sprite-icons sprite-cancel"></span>&nbsp;Cancel',
                click: function(){
                    $(this).dialog("close");
                }
                }]
        });		
		
		
		return false;
	});
	
	/* reply */
	
	$('#forum_menu a.comment-reply').bind('click',function(){

		var _id = $(this).data('id');
		$('#dialog-reply').dialog({
            modal: false,
            autoOpen: true,
            height: 500,
            width: 470,
            resizable: false,
            draggable: false,
            buttons: [{
                html: '<span class="m-t-1 sprite-icons sprite-commend_add"></span>&nbsp;Reply',
            	click: function(){

            		var _dialog = $(this);
            		var _data = $('#form-forum_reply').serialize();

            		$.ajax({
                		url: _root + 'ajax/forum/?act=reply'+ _ajaxext,
                		type: 'post',
                		cache: false,
                		dataType : 'json',
                		data: _data + '&id='+_id,
                		success: function(json){
                			if(json.xhr_status == 1) {
                        		var _json = $('#form-forum_reply').serializeObject();
                  				$('#page_forum').jqoteapp('#page_forum #tmpl_new_message',_json);
                  				$.scrollTo( '#page_forum .st_bottom', 1000 );
                				_dialog.dialog("close");
                			}
                				
                			if(json.xhr_status == 0) {
                                $("#modal_message").attr('title', json.header).html(json.message);
                				openModal();
                			}
                		}
                	});
                }},
                {
                html:'<span class="m-t-1 sprite-icons sprite-cancel"></span>&nbsp;Cancel',
                click: function(){
                    $(this).dialog("close");
                }
                }]
        });		
		
		
		return false;
	});


});